package com.tbsoft.ehr.dao.agency;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.tbsoft.ehr.dao.BaseDaoImpl;
import com.tbsoft.ehr.model.Agency;

public class AgencyDaoImpl extends BaseDaoImpl<Agency> implements AgencyDao{

	public Agency getAgencyById(String id) {
		return (Agency)getHibernateTemplate().get(Agency.class, id);
	}

	public List<Agency> getAllAgency() {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return session.createQuery("from Agency agency").list();
			}
		});
	}

	public List<Agency> getAgencyByName(String name) {
		final String agname = name;
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query q = session.createQuery("from Agency ag where ag.name=?");
				 q.setString(0, agname);
				 return q.list();
			}
		});	
	}

}
